package com.sz.mes.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.sz.mes.entity.warehouse.Lattice;
import com.sz.mes.entity.warehouse.Shelves;
import com.sz.mes.mapper.warehouse.LatticeMapper;
import com.sz.mes.mapper.warehouse.ShelvesMapper;
import com.sz.mes.service.warehouse.LatticeService;
import com.sz.mes.service.impl.BaseCustomServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* 货架格子相关Service实现
* @author hem
* @version 1.0
* @date 2023-05-25
 * Copyright © szhs
*/
@Service
@Slf4j
public class LatticeServiceImpl extends BaseCustomServiceImpl<LatticeMapper, Lattice> implements LatticeService {
    private final ShelvesMapper shelvesMapper;
    private final LatticeMapper latticeMapper;

    public LatticeServiceImpl(ShelvesMapper shelvesMapper, LatticeMapper latticeMapper) {
        this.shelvesMapper = shelvesMapper;
        this.latticeMapper = latticeMapper;
    }

    public JsonResult getDetailByShelveId(Long shelveId) {
        HashMap<String, Object> map = new HashMap<>();
        Shelves shelve = shelvesMapper.selectById(shelveId);
        map.put("col", shelve.getCol());
        map.put("line", shelve.getLine());
        ArrayList<Object> resultList = new ArrayList<>();
        for (Integer i = 1; i <=shelve.getCol(); i++) {
            QueryWrapper<Lattice> wrapper = new QueryWrapper<>();
            wrapper.eq("warehouse_shelves_id",shelveId);
            wrapper.eq("col",i);
            List<Lattice> lattices = latticeMapper.selectList(wrapper);
            resultList.add(lattices);
        }
        map.put("resultList",resultList);
        return JsonResult.OK(map);
    }

    @Override
    public Lattice findById(Long latticeId) {
        QueryWrapper<Lattice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", latticeId);
        queryWrapper.eq("is_deleted", 0);

        return latticeMapper.selectOne(queryWrapper);
    }

    //根据货架名字返回给前端对应格子的id
    @Override
    public JsonResult getLabelById(String label) {
        QueryWrapper<Lattice> latticeQueryWrapper = new QueryWrapper<>();
//        String data = entity.getName();
        latticeQueryWrapper.eq("label", label);
        Lattice lattice = latticeMapper.selectOne(latticeQueryWrapper);
        return JsonResult.OK(lattice.getId());
    }

}
